import { getAllPostType, getPostListLazyLoad, addPost } from "../api/post";

export const post = {
  async getPostType(tapList) {
    try {
      let result = await getAllPostType();
      tapList.value = result.data;
    } catch (err) {
      uni.showToast({
        title: "获取帖子类型失败, 请检查网络",
        icon: "error"
      });
    }
  },
  async getPostList(cursor, limit) {
    try {
      let result = await getPostListLazyLoad(cursor ? cursor : '', limit);
      return result.data;
    } catch (err) {
      uni.showToast({
        title: "获取帖子列表失败, 请检查网络",
        icon: "error"
      });
    }
  },
  async addPost(data) {
    try {
      const postData = {
        title: data.title,
        content: data.content,
        type: data.tapId,
        img: data.imageUrl,
      }
      let result = await addPost(postData);
      return result.code === 200 ? { success: true } : { success: false };
    } catch (err) {
      uni.showToast({
        title: "发布动态失败, 请检查网络",
        icon: "error"
      });
    }
  },
};